<?php
/**
 * 考勤系统App审批记录
 */
namespace app\attendance\controller;

use app\attendance\Base;
use think\Config;
use think\Db;
use think\Loader;
use think\exception\HttpException;
use baseutils\BaseUtil; # 验证类

use com\gdl\kq\interfaces\AttAppApprovalServiceClient;
use com\gdl\kq\dataobject\attapproval\ApprovalAllDTO; 
use com\gdl\kq\dataobject\attapproval\ApprovalDTO; 

class AttAppApproval extends Base
{
	//获取审批流程 仅展示 
	public function approvalMember(){
		$msg =  ['msg'=>'当前网络环境异常，请检查手机网络','code'=>'400','data'=>[]];

		#其他参数
		$platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : '';  #平台id
		$userId = $this->request->param('userId') ? getStr($this->request->param('userId')) : ''; #用户id
		$unitId =  $this->request->param('unitId') ? getStr($this->request->param('unitId')) : ''; #单位id
		$type =  $this->request->param('type') ? getStr($this->request->param('type')) : '1'; #type 1请假流程 2外出流程 3补卡流程 5加班

		$token = $this->request->param('token') ? getStr($this->request->param('token')) : '';
		if(!checkToken($token,$userId)){$msg['msg'] ='用户登录失效，请重新登录'; $msg['code']=501; return json_encode($msg);}
		if(empty($platformId)){$msg['msg'] = "请选择正确的平台"; return json_encode($msg);}

		try{
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
			$api = new AttAppApprovalServiceClient(NULL);
			\ApiClient::build($api);
			
			$approvalDO = new ApprovalDTO();
			$sdkData = ['type'=>$type];
			$approvalDO->userId = $userId; 
			$approvalDO->platformId = $platformId;
			$approvalDO->unitId = $unitId;
			$approvalDO->data = $sdkData;

			$result = $api->approvalMember($approvalDO);
			if($result->success==true){
				$msg['code'] = $result->code;
				$msg['msg'] = $result->message;
				if($result->code==200 && !empty($result->data)){
					$msg['data'] = json_decode($result->data[0]['rule']);
				}
			}

		}catch(Exception $ex){
			$msg['msg'] = $ex->getMessage();
		}
		return json_encode($msg);
	}
	//获取审批流程状态 用于查看 第一视角
	public function approvalStatus(){
		$msg =  ['msg'=>'当前网络环境异常，请检查手机网络','code'=>'400','data'=>[]];

		#其他参数
		$platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : '';  #平台id
		$userId = $this->request->param('userId') ? getStr($this->request->param('userId')) : ''; #用户id
		$unitId =  $this->request->param('unitId') ? getStr($this->request->param('unitId')) : ''; #单位id
		$type =  $this->request->param('type') ? getStr($this->request->param('type')) : ''; #type 1请假流程 2外出流程 3补卡流程 5加班
		$recordId = $this->request->param('recordId') ? getStr($this->request->param('recordId')) : ''; #申请的 请假 外出 补卡id 加班id

		$token = $this->request->param('token') ? getStr($this->request->param('token')) : '';
		if(!checkToken($token,$userId)){$msg['msg'] ='用户登录失效，请重新登录'; $msg['code']=501; return json_encode($msg);}
		if(empty($platformId)){$msg['msg'] = "请选择正确的平台"; return json_encode($msg);}

		try{
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
			$api = new AttAppApprovalServiceClient(NULL);
			\ApiClient::build($api);
			
			$approvalDO = new ApprovalDTO();
			$sdkData = ['type'=>$type, 'recordId'=>$recordId];
			$approvalDO->userId = $userId; 
			$approvalDO->platformId = $platformId;
			$approvalDO->unitId = $unitId;
			$approvalDO->data = $sdkData;

			$result = $api->approvalStatus($approvalDO);
			if($result->success==true){
				$msg['code'] = $result->code;
				$msg['msg'] = $result->message;
				if($result->code==200 && !empty($result->data)){
					$msg['data'] = json_decode($result->data[0]['data']);
				}
			}

		}catch(Exception $ex){
			$msg['msg'] = $ex->getMessage();
		}
		return json_encode($msg);
	}

	//审批状态 第三方视角
	public function showApprovalStatus(){
		$msg =  ['msg'=>'当前网络环境异常，请检查手机网络','code'=>'400','data'=>[]];

		#其他参数
		$platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : '';  #平台id
		$userId = $this->request->param('userId') ? getStr($this->request->param('userId')) : ''; #用户id
		$unitId =  $this->request->param('unitId') ? getStr($this->request->param('unitId')) : ''; #单位id
		$type =  $this->request->param('type') ? getStr($this->request->param('type')) : ''; #type 1请假流程 2外出流程 3补卡流程 5加班
		$recordId = $this->request->param('recordId') ? getStr($this->request->param('recordId')) : ''; #申请的 请假 外出 补卡id 加班id

		$token = $this->request->param('token') ? getStr($this->request->param('token')) : '';
		if(!checkToken($token,$userId)){$msg['msg'] ='用户登录失效，请重新登录'; $msg['code']=501; return json_encode($msg);}
		if(empty($platformId)){$msg['msg'] = "请选择正确的平台"; return json_encode($msg);}

		try{
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
			$api = new AttAppApprovalServiceClient(NULL);
			\ApiClient::build($api);
			
			$approvalDO = new ApprovalDTO();
			$sdkData = ['type'=>$type, 'recordId'=>$recordId];
			$approvalDO->userId = $userId; 
			$approvalDO->platformId = $platformId;
			$approvalDO->unitId = $unitId;
			$approvalDO->data = $sdkData;

			$result = $api->showApprovalStatus($approvalDO);
			if($result->success==true){
				$msg['code'] = $result->code;
				$msg['msg'] = $result->message;
				if($result->code==200 && !empty($result->data)){
					$msg['data'] = json_decode($result->data[0]['data']);
				}
			}

		}catch(Exception $ex){
			$msg['msg'] = $ex->getMessage();
		}
		return json_encode($msg);
	}
	//逐级审核
	public function examineApproval(){
		$msg =  ['msg'=>'当前网络环境异常，请检查手机网络','code'=>'400','data'=>[]];
		#其他参数
		$platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : '';  #平台id
		$userId = $this->request->param('userId') ? getStr($this->request->param('userId')) : ''; #用户id
		$unitId =  $this->request->param('unitId') ? getStr($this->request->param('unitId')) : ''; #单位id
		$type =  $this->request->param('type') ? getStr($this->request->param('type')) : ''; #type 1请假流程 2外出流程 3补卡流程 5加班
		$cardId = $this->request->param('cardId') ? getStr($this->request->param('cardId')) : ''; #层级表id
		$info = $this->request->param('info') ? getStr($this->request->param('info')) : ''; #审批意见
		$status = $this->request->param('status') ? getStr($this->request->param('status')) : ''; #1：拒绝 2：通过

		$token = $this->request->param('token') ? getStr($this->request->param('token')) : '';
		if(!checkToken($token,$userId)){$msg['msg'] ='用户登录失效，请重新登录'; $msg['code']=501; return json_encode($msg);}
		if(empty($platformId)){$msg['msg'] = "请选择正确的平台"; return json_encode($msg);}

		try{
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
			$api = new AttAppApprovalServiceClient(NULL);
			\ApiClient::build($api);
			
			$approvalDO = new ApprovalDTO();
			$sdkData = ['type'=>$type, 'cardId'=>$cardId,'info'=>$info,'status'=>$status];
			$approvalDO->userId = $userId; 
			$approvalDO->platformId = $platformId;
			$approvalDO->unitId = $unitId;
			$approvalDO->data = $sdkData;

			$result = $api->examineApproval($approvalDO);
			if($result->success==true){
				$msg['code'] = $result->code;
				$msg['msg'] = $result->message; 
			}

		}catch(Exception $ex){
			$msg['msg'] = $ex->getMessage();
		}
		return json_encode($msg);
	}
	//查看我的申请
	public function applyList(){
		$msg =  ['msg'=>'当前网络环境异常，请检查手机网络','code'=>'400','data'=>[]];
		#其他参数
		$platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : '21';  #平台id
		$userId = $this->request->param('userId') ? getStr($this->request->param('userId')) : '2'; #用户id
		$unitId =  $this->request->param('unitId') ? getStr($this->request->param('unitId')) : ''; #单位id
		$offset =  $this->request->param('offset') ? getStr($this->request->param('offset')) : 1; #分页 从第一页开始
		$num =  $this->request->param('num') ? getStr($this->request->param('num')) : 10; #每页10条
		$likeTitle = $this->request->param('likeTitle') ? getStr($this->request->param('likeTitle')) : ''; #模糊查询编号，事由
		$status = $this->request->param('status') ? getStr($this->request->param('status')) : ''; # 0：全部 1：审批完成 2：审批中 3：撤销
		$type = $this->request->param('type') ? getStr($this->request->param('type')) : 0; # 0：全部 1：请假 2：外出 3：补卡 5加班

		$token = $this->request->param('token') ? getStr($this->request->param('token')) : '';
		if(!checkToken($token,$userId)){$msg['msg'] ='用户登录失效，请重新登录'; $msg['code']=501; return json_encode($msg);}
		if(empty($platformId)){$msg['msg'] = "请选择正确的平台"; return json_encode($msg);}

		try{
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
			$api = new AttAppApprovalServiceClient(NULL);
			\ApiClient::build($api);
			
			$approvalDO = new ApprovalAllDTO();
			$sdkData = ['type'=>$type, 'likeTitle'=>$likeTitle,'status'=>$status,'likeTitle'=>$likeTitle];
			$approvalDO->userId = $userId; 
			$approvalDO->platformId = $platformId;
			$approvalDO->unitId = $unitId;
			$approvalDO->offset = $offset;
			$approvalDO->num = $num;
			$approvalDO->data = $sdkData;
			$result = $api->applyList($approvalDO);
			if($result->success==true){
				$msg['code'] = $result->code;
				$msg['msg'] = $result->message; 
				if(!empty($result->data)){
					$msg['data'] = json_decode($result->data[0]['list']);	
				}
				
			}

		}catch(Exception $ex){
			$msg['msg'] = $ex->getMessage();
		}
		return json_encode($msg);
	}

	//查看待我审批列表
	public function approvalList(){
		$msg =  ['msg'=>'当前网络环境异常，请检查手机网络','code'=>'400','data'=>[]];
		#其他参数
		$platformId = $this->request->param('platformId') ? getStr($this->request->param('platformId')) : '';  #平台id
		$userId = $this->request->param('userId') ? getStr($this->request->param('userId')) : ''; #用户id
		$unitId =  $this->request->param('unitId') ? getStr($this->request->param('unitId')) : ''; #单位id
		$offset =  $this->request->param('offset') ? getStr($this->request->param('offset')) : 1; #分页 从第一页开始
		$num =  $this->request->param('num') ? getStr($this->request->param('num')) : 10; #每页10条
		$likeTitle = $this->request->param('likeTitle') ? getStr($this->request->param('likeTitle')) : ''; #模糊查询编号，事由
		$status = $this->request->param('status') ? getStr($this->request->param('status')) : ''; # 状态 0：全部 1：待我审批 2：我已审批
		$type = $this->request->param('type') ? getStr($this->request->param('type')) : 0; # 0：全部 1：请假 2：外出 3：补卡 5：加班
		$adopt = $this->request->param('adopt') ? getStr($this->request->param('adopt')) : 0;  #0：全部 1：审批完成 2：审批中 3：撤销

		$token = $this->request->param('token') ? getStr($this->request->param('token')) : '';
		if(!checkToken($token,$userId)){$msg['msg'] ='用户登录失效，请重新登录'; $msg['code']=501; return json_encode($msg);}
		if(empty($platformId)){$msg['msg'] = "请选择正确的平台"; return json_encode($msg);}

		try{
			\ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlCloudSdk'));
			$api = new AttAppApprovalServiceClient(NULL);
			\ApiClient::build($api);
			
			$approvalDO = new ApprovalAllDTO();
			$sdkData = ['type'=>$type, 'likeTitle'=>$likeTitle,'status'=>$status,'likeTitle'=>$likeTitle,'adopt'=>$adopt];
			$approvalDO->userId = $userId; 
			$approvalDO->platformId = $platformId;
			$approvalDO->unitId = $unitId;
			$approvalDO->offset = $offset;
			$approvalDO->num = $num;
			$approvalDO->data = $sdkData;
			$result = $api->approvalList($approvalDO);
			if($result->success==true){
				$msg['code'] = $result->code;
				$msg['msg'] = $result->message; 
				$msg['data']['list'] = [];
				$msg['data']['count']= $result->counts;
				if(!empty($result->data)){
					$msg['data']['list'] = json_decode($result->data[0]['list']);
					$msg['data']['count']= $result->counts;
				}
			}

		}catch(Exception $ex){
			$msg['msg'] = $ex->getMessage();
		}
		return json_encode($msg);
	}
}
